package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.example.qingshengzheng.material.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.ImageEditorPreviewAdapter;
import reduce.fz.com.reduce.bean.ImageEditor;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.PublishParams;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.photo.PhotoWallActivity;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ReduceLog;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.oss.Oss;
import reduce.fz.com.reduce.util.oss.model.OssPic;

/**
 * 发布
 */

public class PubWitnessActivity extends BaseToolBarActivity implements ImageEditorPreviewAdapter.OnAddImageListener {
    private GridView mGridView;
    private List<ImageEditor> mImageEditorList = new ArrayList<ImageEditor>();
    private ImageEditorPreviewAdapter mAdapter;
    private ArrayList<String> selectPaths;
    private TextView mTvFinish;
    private EditText mWitnessContent;
    private Oss oss = Oss.instance(this);
    private TeamService teamService = ServiceBuilder.builderTeamService(this);
    private Long teamid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        teamid = getIntent().getLongExtra("teamid", 0);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pub_witness);
        initWidget();
        initFinishBtn();



    }


    public void initFinishBtn() {
        final LoadingDialog dialog = new LoadingDialog(this);
        dialog.setTitle("准备提交!");
        dialog.show();
        mTvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                oss.uploadPics(selectPaths, new Oss.OssUploadResponse() {
                    @Override
                    public void onSuccess(List<OssPic> successPics, List<OssPic> failedPics) {
                        List<String> pics = new ArrayList<String>();
                        for (int i = 0; i < successPics.size(); i++) {
                            pics.add(successPics.get(i).ossPath);
                        }
                        submitInfo(pics, dialog);

                    }

                    @Override
                    public void onPrgress(int complete, int total) {
                        dialog.setTitle("提交" + complete + "/" + total);
                    }
                });

            }
        });
    }

    public void submitInfo(List<String> pics, final LoadingDialog dialog)

    {
        HashMap map = new HashMap();

        map.put("affimages", pics);
        map.put("desc", mWitnessContent.getText().toString());
        map.put("teamid", teamid);
        teamService.pubWitness(map, new AbResponseListener(new TypeReference<Result>() {
        }) {
            @Override
            public void responseInfo(Result data) {
                if (dialog != null && dialog.isShowing())
                    dialog.dismiss();

            }
        });
    }




    public void initWidget() {
        mTvFinish = (TextView) findViewById(R.id.tv_finish);
        mWitnessContent = (EditText) findViewById(R.id.witness_content);
        mGridView = (GridView) findViewById(R.id.card_publish_selected_list);
        // 初始化自定义的适配器
        mAdapter = new ImageEditorPreviewAdapter(this, mImageEditorList, this);
        // 为mRecyclerView设置适配器
        mGridView.setAdapter(mAdapter);


    }

    public List<ImageEditor> toImageEditor(List<String> paths) {
        List<ImageEditor> editors = new ArrayList<ImageEditor>();
        for (int i = 0; i < paths.size(); i++) {
            ImageEditor temp = new ImageEditor();
            temp.originalPath = paths.get(i);
            editors.add(temp);
        }
        return editors;
    }

    @Override
    public void initListener() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case SELECT_IMAGE:
                final ArrayList<String> paths = data.getStringArrayListExtra(PhotoWallActivity.IMAGE_SELECT_RESULT);
                selectPaths = paths;
                mImageEditorList.addAll(toImageEditor(paths));
                mAdapter.replaceImage(mImageEditorList);
                break;
            default:
                break;
        }


    }

    private final static int SELECT_IMAGE = 100;

    public void selectPic(int maxCount) {
        Intent intent = new Intent(this, PhotoWallActivity.class);
        intent.putExtra(PublishParams.MAX_SELECT_COUNT, maxCount);
        intent.putExtra(PublishParams.HAS_SELECT_LIST, selectPaths);
        startActivityForResult(intent, SELECT_IMAGE);
    }


    @Override
    public void needSelectImage(int maxCount, int needCount) {
        selectPic(maxCount);

    }

    @Override
    public void onEditorClick(int position) {

    }

    @Override
    public void onRemoveImage(int position) {
        if (position < mImageEditorList.size())
            mImageEditorList.remove(position);
    }
}
